package com.smart.manager.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.ExceptionTips;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.manager.system.entities.body.RSystemOrgan;
import com.smart.manager.system.entities.models.SystemOrgan;
import com.smart.manager.system.repository.OrganRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 系统机构- 前端API机构
 * 当前页面的机构需要超级管理员的角色才能登录
 *
 * @author xiaxuqiu
 * @date 2022/11/18
 */
@Api(tags =  "机构管理")
@RestController
@RequestMapping(SmartConstant.MANAGER_API_PREFIX + "system/org/organ")
@RequiredArgsConstructor
public class OrganApi {

    private final OrganRepository repository;


    @GetMapping("list")
    @ApiOperation(value = "查询机构")
    @SaCheckPermission("system:org:organ:list")
    public AjaxResult<List<SystemOrgan>> getList() {
        List<SystemOrgan> result = repository.getFullList();
        return AjaxResult.data( result );
    }


    @ApiOperation(value = "添加机构")
    @PostMapping("add")
    @SaCheckPermission("system:org:organ:add")
    public AjaxResult<String> add(@RequestBody @Validated RSystemOrgan.EditBody data) {
        repository.add( data );
        return AjaxResult.success();
    }


    @ApiOperation(value = "修改机构")
    @PutMapping("update/{id}")
    @SaCheckPermission("system:org:organ:update")
    public AjaxResult<String> update(@PathVariable Long id, @RequestBody @Validated RSystemOrgan.EditBody data) {
        repository.update( id, data );
        return AjaxResult.success();
    }


    @ApiOperation(value = "删除机构")
    @DeleteMapping("delete")
    @SaCheckPermission("system:org:organ:delete")
    public AjaxResult<Integer> delete(@RequestBody @NotEmpty(message = ExceptionTips.PK_EMPTY) List<Long> ids) {
        int i = repository.deleteByIds( ids );
        return AjaxResult.delSuccess( i );
    }

}
